﻿using System;
using System.Web.UI.WebControls;
using TraditionalMalteseCuisine.App_Code.Functions;

namespace TraditionalMalteseCuisine.UserControls
{
    public partial class CalendarControl : System.Web.UI.UserControl
    {
        #region Page Load
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #endregion


        #region Events
        protected void imgCalendar_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if ((div_calendar_control.Style["display"] != null) && (div_calendar_control.Style["display"].Equals("none")))
            {
                if (IsValid())
                {
                    cldDate.SelectedDate = SelectedDate;
                    cldDate.VisibleDate = cldDate.SelectedDate;
                }
                else
                    btnToday_Click(btnToday, null);

                div_calendar_control.Style["display"] = "block";
            }
            else
                div_calendar_control.Style["display"] = "none";
        }

        protected void cldDate_SelectionChanged(object sender, EventArgs e)
        {
            cldDate.SelectedDate = cldDate.SelectedDate;
            cldDate.VisibleDate = cldDate.SelectedDate;

            cldDate_VisibleMonthChanged(cldDate, null);

            imgCalendar_Click(imgCalendar, null);
        }

        protected void cldDate_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
        {
            txtYear.Text = string.Format("{0:0000}", cldDate.VisibleDate.Year);

            txtDateTime.Text = DateTime_Functions.GetShortDate(cldDate.VisibleDate.ToString());
        }
        #endregion


        #region Buttons
        protected void btnToggleYear_Click(object sender, EventArgs e)
        {
            Button button = ((Button)sender);

            if (button.Text.Equals("<"))
                cldDate.SelectedDate = cldDate.VisibleDate.AddYears(-1);
            else
                cldDate.SelectedDate = cldDate.VisibleDate.AddYears(1);

            cldDate.SelectedDate = cldDate.SelectedDate;
            cldDate.VisibleDate = cldDate.SelectedDate;

            cldDate_VisibleMonthChanged(cldDate, null);
        }

        protected void btnToday_Click(object sender, EventArgs e)
        {
            cldDate.SelectedDate = cldDate.TodaysDate;

            cldDate_SelectionChanged(cldDate, null);
        }
        #endregion
        

        #region Methods
        public bool IsValid()
        {
            if (Validation_Functions.IsDateTime(txtDateTime.Text))
            {
                txtDateTime.Text = DateTime.Parse(txtDateTime.Text).ToString("dd-MMM-yyyy");
                txtYear.Text = DateTime.Parse(txtDateTime.Text).ToString("yyyy");
                csvDateTime.IsValid = true;
            }
            else
            {
                txtDateTime.Text = string.Empty;
                csvDateTime.IsValid = false;
            }

            return csvDateTime.IsValid;
        }
        #endregion


        #region Properties
        public DateTime SelectedDate
        {
            get { return ((IsValid()) ? DateTime.Parse(txtDateTime.Text) : cldDate.SelectedDate); }
        }
        #endregion
    }
}